package com.ljz.filter;

import com.ljz.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.ArrayList;
import java.util.List;


@WebListener
public class ServletContextEvent implements ServletContextListener, HttpSessionAttributeListener {
    ServletContext application = null;
    @Override
    public void contextInitialized(javax.servlet.ServletContextEvent sce) {
        List<User> list = new ArrayList<User>();
        application = sce.getServletContext();
        application.setAttribute("list",list);
    }

    @Override
    public void contextDestroyed(javax.servlet.ServletContextEvent servletContextEvent) {

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent hbe) {
        User user = (User) hbe.getSession().getAttribute("user");
        List<User> list = (List<User>) application.getAttribute("list");
        list.add(user);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent hbe) {
        User user = (User) hbe.getValue();
        List<User> list = (List<User>) application.getAttribute("list");
        list.remove(user);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
